import axios from "axios";
import { ElMessage } from "element-plus";

// 创建 axios 实例
const service = axios.create({
  baseURL: "http://localhost:3030",
  timeout: 50000,
  headers: {
    "Content-Type": "application/json;charset=utf-8",
  },
});

// 响应拦截器
service.interceptors.response.use(
  (response) => {
    const { code, msg } = response.data;
    if (code === 0) {
      return response.data;
    }
    // 响应数据为二进制流类型
    if (
      response.headers["content-type"] != "application/json" ||
      response.data instanceof Blob
    ) {
      return response;
    }
  },
  (error: any) => {
    if (axios.isCancel(error)) {
      console.log("请求取消", error.message);
    } else if (error) {
      console.log(error || "系统出错");
    }
  }
);

// 导出 axios 实例
export default service;
